//
//  String.swift
//  Swift_base_grammar
//
//  Created by lantaiyuan on 2017/9/13.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//  字符串 操作

import Foundation
import UIKit


/// 字符串拼接
func string1() {
    let name:String = "李四"
    let age = 19
    // 字符串的拼接 \(变量名) 方式
    print("\(name) \(age)")
    
}

/// 字符串遍历
func string2() {
    let str = "hello world!"
    for c in str.characters {
        print(c)
    }
}

/// 字符串的长度
func string3() {
    let str = "隔壁老王貌美如花"
    // 一个中文的 UTF 对应3个字节，返回的C语言的字符串长度
    print(str.lengthOfBytes(using: .utf8))
    // 返回字符串中的字符数量 - 字符串的长度！
    print(str.characters.count)
}

/// 格式化字符串
func string4() {
    let h = 8
    let m = 5
    let s = 6
    
    print("\(h)\(m)\(s)")
    // 如果需要使用格式字符串的时候，可以单独使用 String(format: xxx)
    let dateString = String.init(format: "%02d:%02d:%02d", [h,m,s])
    print(dateString)
    
    // 可以直接在格式字符串后面添加 参数，用 逗号分隔
//    let dateString2 = String(format: "%02d:%02d:%02d", h, m, s)
//    print(dateString2)
    
}

/// 字符串的子串，提示：在 Swift 中 String 取子串不是特别好写，建议使用 NSString
func string5() {
    
    let str = "hello world"
    let s1 = (str as NSString).substring(with: NSMakeRange(2, 5))
    print(s1)
    
    // --- 提示 -- 以下代码仅供参考，Swift语法中，变化最频繁
    let s2 = str.substring(from: "1234".endIndex)
    print(s2)
    
    // 指定范围 ..<
    // advancedBy 正数，向右移动，负数向左移动
    let startIndex = str.index(str.startIndex, offsetBy: 2)//0
    let endIndex = str.index(str.endIndex, offsetBy: -2)//11
        
    let range = startIndex..<endIndex
    
    let s3 = str.substring(with: range)
    print(s3)
    
}
func string6() {
    var string = "Hello-Swift"
    //获取某个下标后一个下标对应的字符 char="e"
    let char = string[string.index(after: string.startIndex)]
    print(char)
    //获取某个下标前一个下标对应的字符 char2 = "t"
    let char2 = string[string.index(before: string.endIndex)]
    print(char2)
    //通过范围获取字符串中的一个子串 Hello
    let subString = string[string.startIndex...string.index(string.startIndex, offsetBy: 4)]
    print(subString)
    let subString2 = string[string.index(string.endIndex, offsetBy: -5)..<string.endIndex]
    print(subString2)
    //获取某个子串在父串中的范围
    let range = string.range(of: "Hello")
    print(range!)
    //追加字符串操作 此时string = "Hello-Swift! Hello-World"
    string.append(" Hello-World")
    
    //在指定位置插入一个字符 此时string = "Hello-Swift!~ Hello-World"
    string.insert("~", at: string.index(string.startIndex, offsetBy: 12))
    
    //在指定位置插入一组字符 此时string = "Hello-Swift!~~~~ Hello-World"
    string.insert(contentsOf: ["~","~","~"], at: string.index(string.startIndex, offsetBy: 12))
    
    //在指定范围替换一个字符串 此时string = "Hi-Swift!~~~~ Hello-World"
    string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
    
    //在指定位置删除一个字符 此时string = "Hi-Swift!~~~~ Hello-Worl"
    string.remove(at: string.index(before: string.endIndex))

   //删除指定范围的字符 此时string = "Swift!~~~~ Hello-Worl"
   string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
   
    var string2 = "My name is Jaki"
    //全部转换为大写
    string2 = string2.uppercased()
    //全部转换为小写
    string2 = string2.lowercased()
    
    print(string2)
}
